package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;

	@PostMapping("/trade")
	@Operation(summary = "确认订单")
	@TsLogin
	public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo) {
		Long userId = AuthContextHolder.getUserId();
		OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo,userId);
		return Result.ok(orderInfoVo);
	}

	@PostMapping("/submitOrder")
	@Operation(summary = "订单结算")
	@TsLogin
	public Result<Map<String, Object>> submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {
		//  获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		//  调用服务层方法
		String orderNo = orderInfoService.submitOrder(orderInfoVo, userId);
		Map<String, Object> map = new HashMap();
		map.put("orderNo", orderNo);
		//	返回数据
		return Result.ok(map);
	}

	@GetMapping("/getOrderInfo/{orderNo}")
	@Operation(summary = "根据订单号查询订单信息")
	public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
		OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
		return Result.ok(orderInfo);
	}

	@GetMapping("/findUserPage/{pageNo}/{pageSize}")
	@Operation(summary = "根据用户id分页查询订单列表")
	@TsLogin
	public Result<IPage<OrderInfo>> findUserPage(@PathVariable Long pageNo,
										  @PathVariable Long pageSize,
												 @RequestParam(required = false)String orderStatus) {
		Long userId = AuthContextHolder.getUserId();
		Page<OrderInfo> page = new Page(pageNo, pageSize);
		orderInfoService.findUserPage(page, userId, orderStatus);
		return Result.ok(page);
	}
}

